package com.hq.bus.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hq.bus.domain.Car;
import com.hq.sys.util.DataGridView;
import com.hq.bus.mapper.CarMapper;
import com.hq.bus.service.CarService;
import com.hq.bus.vo.CarVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.weekend.Weekend;

import java.util.List;
import java.util.Objects;

/**
 * @BelongsPackage: com.hq.bus.service.impl
 * @Author: jiangjunjie
 * @CreateTime: 2024-12-16  14:30
 * @Description: TODO
 * @Version: 1.8
 */
@Service
@Transactional
public class CarServiceImpl implements CarService {

    @Autowired
    private CarMapper carMapper;
    @Override
    public DataGridView queryAllCar(CarVo carVo) {
        Page<Object> page = PageHelper.startPage(carVo.getPage(), carVo.getLimit());
        Weekend<Car> weekend = Weekend.of(Car.class);
        if (StrUtil.isNotBlank(carVo.getCarnumber())){
            weekend.weekendCriteria().andEqualTo(Car::getCarnumber, carVo.getCarnumber());
        }
        if (StrUtil.isNotBlank(carVo.getCartype())){
            weekend.weekendCriteria().andLike(Car::getCartype, "%" + carVo.getCartype() + "%");
        }
        if(StrUtil.isNotBlank(carVo.getColor())){
            weekend.weekendCriteria().andLike(Car::getColor, "%" + carVo.getColor() + "%");
        }
        if (StrUtil.isNotBlank(carVo.getDescription())){
            weekend.weekendCriteria().andLike(Car::getDescription, "%" + carVo.getDescription() + "%");
        }
        if (Objects.nonNull(carVo.getIsrenting())){
            weekend.weekendCriteria().andEqualTo(Car::getIsrenting, carVo.getIsrenting());
        }
        List<Car> data = carMapper.selectByExample(weekend);
        return new DataGridView(page.getTotal(), data);
    }

    @Override
    public CarVo queryCarById(String carnumber) {
       carMapper.selectByPrimaryKey(carnumber);
        return null;
    }

    @Override
    public int addCar(CarVo carVo) {
        return carMapper.insertSelective(carVo);
    }

    @Override
    public void deleteCar(String carnumber) {
        carMapper.deleteByPrimaryKey(carnumber);
    }


    @Override
    public void deleteBatchCar(String[] CarNumbers) {
        for (String carNumber : CarNumbers) {
            carMapper.deleteByPrimaryKey(carNumber);
        }

    }

    @Override
    public int updateCar(CarVo carVo) {
        return carMapper.updateByPrimaryKey(carVo);
    }
}
